using System;
using System.Collections.Generic;
using System.Text;

namespace BAFIRCSharp
{
    public struct ChannelMode
    {
        public string Mode;
        public string Args;

        public ChannelMode(string Mode)
        {
            this.Mode = Mode;
            this.Args = "";
        }

        public ChannelMode(string Mode, string Args)
        {
            this.Mode = Mode;
            this.Args = Args;
        }
    }

    public class ModeTracker
    {
        private string _ModeList = ""; public string ModeList { get { return _ModeList; } set { AddModeString(value); } }
        public List<ChannelMode> Modes;

        public ModeTracker()
        {
            this.ModeList = "";
            Modes = new List<ChannelMode>();
        }

        public ModeTracker(string ModeList)
        {
            this.ModeList = ModeList;
            Modes = new List<ChannelMode>();
        }

        public void AddModeString(string MS)
        {
            string Args = MS.Substring(MS.IndexOf(' ') + 1);
            MS = MS.Substring(0, MS.IndexOf(' '));
            int ArgPos = 0;
            char LastMode = ' ';
            for (int i = 0; i < MS.Length; ++i) // step through mode string 1 char at a time
            {
                if (MS[i] == '+')
                    LastMode = '+';
                else if (MS[i] == '-')
                    LastMode = '-';
                else // it's a mode
                {
                    if (LastMode == ' ') // WTF?
                    {
                    }
                    else
                    {
                        // Search for mode.
                        string ThisArg = "";
                        foreach(ChannelMode mode in Modes)
                        {
                            if (mode.Mode[0] == MS[i])
                            {
                                if (LastMode == '+') // if it is there, update it. Else add it
                                {
                                    if (ArgPos < Args.Length)
                                    {
                                        int ArgEnd = Args.IndexOf(' ', ArgPos);
                                        if (ArgEnd == -1)
                                            ArgEnd = Args.Length;
                                        ThisArg = Args.Substring(ArgPos, ArgEnd);
                                        ArgPos = ArgEnd + 1;
                                    }

                                    Modes.Remove(mode);
                                }
                                else if (LastMode == '-') // get rid of it
                                    Modes.Remove(mode);
                                break;
                            }
                        }
                        if (LastMode == '+')
                        {
                            // Add it.
                            Modes.Add(new ChannelMode("" + MS[i], ThisArg));
                        }
                    }
                }
            }

            // Construct _ModeList
            UpdateModeList();
        }

        public void UpdateModeList()
        {
            List<ChannelMode> TempList1 = new List<ChannelMode>();
            List<ChannelMode> TempList2 = new List<ChannelMode>();
            foreach(ChannelMode mode in Modes)
            {
                if (mode.Args != string.Empty)
                    TempList1.Add(mode);
                else
                    TempList2.Add(mode);
            }

            List<ChannelMode> SortedModes = new List<ChannelMode>();
            foreach (ChannelMode mode in TempList1)
            {
                SortedModes.Add(mode);
            }
            foreach (ChannelMode mode in TempList2)
            {
                SortedModes.Add(mode);
            }

            ModeList = "+";
            string ArgList = "";
            foreach (ChannelMode mode in SortedModes)
            {
                if (mode.Args != string.Empty)
                    ArgList += " " + mode.Args;
                ModeList += mode.Mode;
            }
            ModeList += ArgList;
        }
    }
}
